package codeTools;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.sql.Blob;
import java.sql.SQLException;

import javax.imageio.ImageIO;
import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialException;

public class MyEncoder {//编码
	public static Blob pictureToBlob(String picture) throws IOException, SerialException, SQLException {
		//获取图片的类型
		String pictureFileNameSplit[] = "1.jpg".split("\\.");//注意“.”是特殊符号。
		String pictureType = pictureFileNameSplit[pictureFileNameSplit.length - 1];
		
		File file = new File(picture);
		BufferedImage bufferedImage = ImageIO.read(file);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ImageIO.write(bufferedImage, pictureType, out);
		byte[] byteArray = out.toByteArray();
		Blob blob = new SerialBlob(byteArray);
		
		return blob;
	}
}
